

import java.io.*;
import java.net.*;
import java.net.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.io.*;

public class Server implements Runnable
{  
	private ServerSocket server = null;
   private Thread thread = null;
   private ServerThread client = null;
   private int port = 2004;
   ObjectOutputStream out;
	ObjectInputStream in;
	String message;
	private HashMap<Integer, String> userInfo = new HashMap();
	private HashMap<Integer, String> userGroup = new HashMap();
		private DatagramSocket UDPSocket;
   UserInfo Info;
   private int ID;
   
   public Server(){
	   UserInfo info = new UserInfo();
   this.Info = info;
   this.userInfo= info.getUserInfo();
   this.userGroup= info.getUserGroup();
   
    try
      {  
    	System.out.println("Server starting on port " + port + ", please wait  ...");
         server = new ServerSocket(port);  
         System.out.println("Server started: " + server);
         start();
      }
      catch(IOException ioe)
      {  System.out.println(ioe); }
   }
   
   public void run()
   {  
	   while (thread != null)
      {  try
         {  
    	  System.out.println("Waiting for a client ..."); 
         Socket TCPSocket = server.accept();
         UDPSocket = new DatagramSocket(TCPSocket.getPort());
           System.out.println("Just connected to " + TCPSocket.getRemoteSocketAddress());
            addThread(TCPSocket, UDPSocket );
         }
         catch(IOException ie)
         {  System.out.println("Acceptance Error: " + ie); }
      }
   }
   
   
   /**
    * Adds new thread
    * @param TCPsocket
    * @param UDPsocket
    */
   public void addThread(Socket TCPsocket, DatagramSocket UDPsocket)
   { 
	   
		ID++;
	   client = new ServerThread(this, TCPsocket, ID, UDPsocket);
	   int port = TCPsocket.getPort();
	   	   System.out.println("Client accepted at port: " + port);
	   userInfo.put(ID, (Info.stringParse(String.valueOf(TCPsocket.getRemoteSocketAddress()))));
      try
      {  
    	  client.open();
          client.start();
      }
      catch(IOException ioe)
      {  System.out.println("Error opening thread: " + ioe); }
   }
   
   public void addToGroup(int ID, String group){
	   System.out.println("user is added to groups "+ group);
	   Info.addUserToGroup(ID, group);
	   System.out.println(Info.getUserGroup());
	  
   } 
   
   /**
    * Starts the thread
    */
   public void start()
   {  
	   if (thread == null)
      {  
		   thread = new Thread(this); 
         thread.start();
        
      }
   }

   /**
    *Stops the thread
    */
   public void stop()
   {  
	   if (thread != null)
      {  
		   thread.stop(); 
         thread = null;
      }
   }
   
   /**
    * main
    * @param args
    * @throws IOException
    */
   public static void main(String args[]) throws IOException{
		
	    Server server = new Server();
	}
   
}
